package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.Login;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;

import java.util.List;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user/userInfo")
@SuppressWarnings({"all"})
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private BeanNameUrlHandlerMapping beanNameHandlerMapping;


    /**
     * 处理用户购买记录
     *
     * @param userPaidRecordVo
     * @return
     */
    @Operation(summary = "处理用户购买记录")
    @PostMapping("/savePaidRecord")
    public Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo) {
        //  调用服务层方法;
        userInfoService.savePaidRecord(userPaidRecordVo);
        //  返回数据：
        return Result.ok();
    }

    /**
     * 获取专辑已支付的声音Id集合列表
     *
     * @param albumId
     * @return
     */
    @Login
    @Operation(summary = "获取专辑已支付的声音Id集合列表")
    @GetMapping("/findUserPaidTrackList/{albumId}")
    public Result<List<Long>> findUserPaidTrackList(@PathVariable Long albumId) {
        //  获取用户Id
        Long userId = AuthContextHolder.getUserId();
        //  调用服务层方法
        List<Long> userPaidTrackList = userInfoService.findUserPaidTrackList(albumId, userId);
        //  返回数据
        return Result.ok(userPaidTrackList);

    }

    /**
     * 判断用户是否购买过专辑
     *
     * @param albumId
     * @return
     */
    @Login
    @Operation(summary = "判断用户是否购买过专辑")
    @GetMapping("/isPaidAlbum/{albumId}")
    public Result<Boolean> isPaidAlbum(@PathVariable("albumId") Long albumId) {
        System.out.println("来啦老弟");
        //  获取用户Id
        Long userId = AuthContextHolder.getUserId();
        //  调用服务层方法;
        Boolean flag = userInfoService.isPaidAlbum(userId, albumId);
        System.out.println("flag = " + flag);
        //  返回数据：
        return Result.ok(flag);
    }

    /**
     * 获取用户购买的声音
     *
     * @param albumId
     * @return
     */
    @Login
    @Operation(summary = "获取用户购买的声音")
    @PostMapping("/getUserPaidTrack/{albumId}")
    public Result<List<Integer>> getUserPaidTrack(@PathVariable Long albumId, @RequestBody List<Long> trackIdList) {
        //获取用户id
        Long userId = AuthContextHolder.getUserId();
        //调用服务层方法
        List<Integer> IdList = userInfoService.getUserPaidTrack(userId, albumId, trackIdList);
        //返回结果
        return Result.ok(IdList);
    }

    /**
     * 根据用户id查询用户信息
     *
     * @param userId
     * @return
     */
    @Operation(summary = "根据用户id查询用户信息")
    @GetMapping("/getUserInfoVo/{userId}")
    public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId) {
        //调用服务层函数
        UserInfo userInfo = userInfoService.getById(userId);
        //封装数据
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);
        //返回结果
        return Result.ok(userInfoVo);
    }
}

